    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tfredes.roteador;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import tfredes.roteador.pacote.Pacote;
import tfredes.utils.Constantes;
import tfredes.utils.Utils;

/**
 *
 * @author Rodrigo
 */
public class Roteador {

    private String ipLocal;

    public void run() {
        try {
            DatagramSocket serverSocket = new DatagramSocket(Constantes.PORTA_DO_ROTEADOR);

            byte[] receiveData = new byte[1024];
            while (true) {


                // declara o pacote a ser recebido
                DatagramPacket receivePacket = new DatagramPacket(receiveData,
                        receiveData.length);

                // recebe o pacote do cliente
                serverSocket.receive(receivePacket);

                // pega os dados, o endereo IP e a porta do cliente
                // para poder mandar a msg de volta
                String sentence = new String(receivePacket.getData());
                
                Pacote pacote = Utils.convertStringToPacote(sentence);
                
                DatagramSocket clientSocket = new DatagramSocket();

                DatagramPacket packet = new DatagramPacket(sentence.getBytes(),
                        sentence.getBytes().length, InetAddress.getByName(pacote.getIpDestino()), pacote.getPortaDestino());
                clientSocket.send(packet);
                clientSocket.close();

            }
        } catch (Exception ex) {
            ex.printStackTrace();
     
            System.out.println("deu merda");
        }
    }
}
